﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUCircle
    {
        /// <summary>
        /// Get All the Circle of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Circle> GetAllCircle(bool searchInDeleted)
        {
            List<Circle> circles = new List<Circle>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Circle in db.Circle
                              where Circle.cir_isdeleted == searchInDeleted
                              select Circle;
                foreach (Circle tmp in results)
                    circles.Add(tmp);
            }

            return circles;
        }

        /// <summary>
        /// Get the Circle by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Circle GetCircleByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var cir = from Circle in db.Circle
                              where Circle.cir_id == id
                              select Circle;
                    foreach (Circle tmp in cir)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// cir a Circle into the database.
        /// </summary>
        /// <param name="cir"></param>
        /// <returns></returns>
        public static Circle Circlerea(Circle cir)
        {
            if (cir != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Circle ncir = db.Circle.Add(cir);
                    db.SaveChanges();
                    return ncir;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Circle of the database.
        /// </summary>
        /// <param name="cir"></param>
        /// <returns></returns>
        public static bool RemoveCircle(Circle cir)
        {
            if (cir != null)
            {
                cir.cir_isdeleted = true;
                if (UpdateCircle(cir) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Circle into the database.
        /// </summary>
        /// <param name="cir"></param>
        /// <returns></returns>
        public static Circle UpdateCircle(Circle cir)
        {
            if (cir != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Circle in db.Circle
                                  where Circle.cir_id == cir.cir_id
                                  select Circle;

                    foreach (var tmp in current)
                    {
                        tmp.cir_label = cir.cir_label;
                        tmp.usr_id = cir.usr_id;
                        tmp.cir_isdeleted = cir.cir_isdeleted;

                        db.SaveChanges();
                    }

                    cir = current as Circle;
                }
            }

            return cir;
        }
    }
}
